package com.lrkj.payfee.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.lrkj.common.core.domain.entity.SysUser;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.LrkjDateUtil;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.payfee.domain.FiscalRecord;
import com.lrkj.payfee.mapper.FiscalRecordMapper;
import com.lrkj.payfee.service.IFiscalRecordService;
import com.lrkj.payfee.service.IRoomNormService;
import com.lrkj.payfee.service.IRuleCostService;
import com.lrkj.payfee.service.ITollCenterService;
import com.lrkj.payfee.vo.CurrentPeriodFeeVo;
import com.lrkj.property.domain.Community;
import com.lrkj.property.service.ICommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 结账记录Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-28
 */
@Service
public class FiscalRecordServiceImpl implements IFiscalRecordService {
    @Autowired
    private FiscalRecordMapper fiscalRecordMapper;

    @Autowired
    private ICommunityService communityService;

    @Autowired
    private IRoomNormService roomNormService;

    @Autowired
    private IRuleCostService ruleCostService;

    @Autowired
    private ITollCenterService tollCenterService;

    /**
     * 查询结账记录
     *
     * @param id 结账记录ID
     * @return 结账记录
     */
    @Override
    public FiscalRecord selectFiscalRecordById(String id) {
        return fiscalRecordMapper.selectFiscalRecordById(id);
    }

    /**
     * 查询结账记录列表
     *
     * @param fiscalRecord 结账记录
     * @return 结账记录
     */
    @Override
    public List<FiscalRecord> selectFiscalRecordList(FiscalRecord fiscalRecord) {
        return fiscalRecordMapper.selectFiscalRecordList(fiscalRecord);
    }

    /**
     * 新增结账记录
     *
     * @param fiscalRecord 结账记录
     * @return 结果
     */
    @Override
    public int insertFiscalRecord(FiscalRecord fiscalRecord) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        fiscalRecord.setCreateBy(user.getNickName());
        fiscalRecord.setCreateTime(DateUtils.getNowDate());
        fiscalRecord.setId(IdUtils.simpleUUID());
        return fiscalRecordMapper.insertFiscalRecord(fiscalRecord);
    }

    /**
     * 修改结账记录
     *
     * @param fiscalRecord 结账记录
     * @return 结果
     */
    @Override
    public int updateFiscalRecord(FiscalRecord fiscalRecord) {
        fiscalRecord.setUpdateTime(DateUtils.getNowDate());
        return fiscalRecordMapper.updateFiscalRecord(fiscalRecord);
    }

    /**
     * 批量删除结账记录
     *
     * @param ids 需要删除的结账记录ID
     * @return 结果
     */
    @Override
    public int deleteFiscalRecordByIds(String[] ids) {
        return fiscalRecordMapper.deleteFiscalRecordByIds(ids);
    }

    /**
     * 删除结账记录信息
     *
     * @param id 结账记录ID
     * @return 结果
     */
    @Override
    public int deleteFiscalRecordById(String id) {
        return fiscalRecordMapper.deleteFiscalRecordById(id);
    }


    /**
     * 结账前检查
     *
     * @param fiscalRecord
     * @return 结果
     */
    @Override
    public Map<String, Object> fiscalBeforeCheck(FiscalRecord fiscalRecord) {
        Map<String, Object> resultMap = new HashMap<>();
        Community community = communityService.selectCommunityById(fiscalRecord.getCommunityId());
        if (null == community) {
            throw new BaseException("获取小区信息异常,请刷新页面,重新进行结账。");
        }
        if (StringUtils.isEmpty(community.getFiscalPeriod())) {
            throw new BaseException("小区当前账期,没有做初始化设置,请联系管理员前往小区信息,初始化当前账期。");
        }
        List<CurrentPeriodFeeVo> currentPeriodFeeVos = roomNormService.currentPeriodFee(new CurrentPeriodFeeVo(fiscalRecord.getFiscalPeriod(), fiscalRecord.getCommunityId()));
        resultMap.put("currentPeriodFeeVos", currentPeriodFeeVos);
        return resultMap;
    }

    @Override
    @Transactional
    public Community fiscal(FiscalRecord fiscalRecord) {
        Community community = communityService.selectCommunityById(fiscalRecord.getCommunityId());
        if (null == community) {
            throw new BaseException("获取小区信息异常,请刷新页面,重新进行结账。");
        }
        if (StringUtils.isEmpty(community.getFiscalPeriod())) {
            throw new BaseException("小区当前账期,没有做初始化设置,请联系管理员前往小区信息,初始化当前账期。");
        }
        Date fiscalPeriodDate = null;
        Date lastMonthDay = null;
        try {
            fiscalPeriodDate = LrkjDateUtil.getDateFromString(fiscalRecord.getFiscalPeriod(), "yyyy-MM");
            lastMonthDay = LrkjDateUtil.getLastMonthDay(fiscalPeriodDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取下一个账期
        Date nextFiscalPeriod = LrkjDateUtil.stepMonth(fiscalPeriodDate, 1);
        community.setFiscalPeriod(LrkjDateUtil.getFormatTimeString(nextFiscalPeriod, "yyyy-MM"));
        communityService.initFiscalPeriod(community);
        //新增结账记录
        this.insertFiscalRecord(fiscalRecord);
        return community;
    }

    //反结账
    @Override
    @Transactional
    public Community backFiscal(FiscalRecord fiscalRecord) {
        Community community = communityService.selectCommunityById(fiscalRecord.getCommunityId());
        if (null == community) {
            throw new BaseException("获取小区信息异常,请刷新页面,重新进行结账。");
        }
        if (StringUtils.isEmpty(community.getFiscalPeriod())) {
            throw new BaseException("小区当前账期,没有做初始化设置,请联系管理员前往小区信息,初始化当前账期。");
        }
        Date fiscalPeriodDate = DateUtil.parse(community.getFiscalPeriod(), "yyyy-MM");

        //获取上一个账期
        Date theFiscalPeriod = LrkjDateUtil.stepMonth(fiscalPeriodDate, -1);
        community.setFiscalPeriod(DateUtil.format(theFiscalPeriod, "yyyy-MM"));
        communityService.initFiscalPeriod(community);

        fiscalRecord.setFiscalPeriod(community.getFiscalPeriod());
        //新增结账记录
        this.insertFiscalRecord(fiscalRecord);
        return community;
    }

    /**
     * 批量新增结账记录信息
     *
     * @param fiscalRecords 结账记录对象
     */
    public int batchAddData(List<FiscalRecord> fiscalRecords) {
        return fiscalRecordMapper.batchAddData(fiscalRecords);
    }
}
